
package com.chuhay.elemwar;

import java.awt.Color;
import java.awt.Graphics2D;

import static com.chuhay.elemwar.ElemWarConstants.*;

import com.threerings.media.sprite.Sprite;

public class HexSprite extends Sprite {
   /** Reference to the hex associated with this sprite */
   protected ElemWarObject.Hex _hex;

   /**
    * Creates a hex sprite to display the supplied game piece.
    */
   public HexSprite (ElemWarObject.Hex hex) {
      super(SIZE, SIZE);
      updateHex(hex);
   }

   public String toString () {
      return _hex.toString();
   }

   /**
    * Called when the piece we are displaying has been updated.
    */
   public void updateHex (ElemWarObject.Hex hex) {
      // keep track of our hex
      _hex = hex;

      // set our location based on the grid location
      setLocation(_hex.x - CORNERDIST, _hex.y - CORNERDIST);

      // Draw board hexes below all the other hexes
      switch (_hex.hexArea) {
      case AREA_BOARD:
         setRenderOrder(-10);
         break;
      default:
         setRenderOrder(10);
         break;
      }
      // force a redraw on creation
      invalidate();
   }

   @Override
   // from Sprite
   public void paint (Graphics2D gfx) {
      // Points defining the hex to be drawn
      int [] xPoints = {_hex.x, _hex.x + CORNERDIST, _hex.x + CORNERDIST, _hex.x, _hex.x - CORNERDIST, _hex.x - CORNERDIST};
      int [] yPoints = {_hex.y - CORNERDIST, _hex.y - CORNERDIST / 2, _hex.y + CORNERDIST / 2, _hex.y + CORNERDIST, _hex.y + CORNERDIST / 2, _hex.y - CORNERDIST / 2};

      // Fill the hex area
      Color c = null;

      switch (_hex.hexType) {
      case HEX_FIRE:
         // Fire is red
         c = new Color(255, 128, 128);
         break;
      case HEX_ICE:
         // Ice is blue
         c = new Color(128, 128, 255);
         break;
      case HEX_EMPTY:
         // Board hexes are yellow, goal hexes are gray
         if (_hex.hexArea == AREA_BOARD)
            c = Color.YELLOW;
         else
            c = Color.DARK_GRAY;
         break;
      case HEX_FILLED:
         // Filled hexes are black
         c = Color.BLACK;
         break;
      default:
         // We should never get here
         c = Color.ORANGE;
      }

      // Modify hex color based on selection status
      switch (_hex.selected) {
      case SELECT_SELECTED:
         // Selected hex is brightest, use Color.brighter()
         if (_hex.hexType == HEX_EMPTY)
            c = c.brighter().brighter().brighter().brighter();
         else
            c = c.brighter().brighter();
         // Darken color using alpha channel
         c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 192);
         break;
      case SELECT_VALID:
         // Valid move hex is second brightest, use Color.brighter()
         if (_hex.hexType == HEX_EMPTY)
            c = c.brighter().brighter();
         else
            c = c.brighter();
         // Darken color using alpha channel
         c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 128);
         break;
      default:
         // Hex is unselected, make it the darkest
         if (_hex.hexType != HEX_FILLED)
            c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 64);
         break;
      }

      // Set current graphics color to modified color, then draw hex
      gfx.setColor(c);
      gfx.fillPolygon(xPoints, yPoints, xPoints.length);

      // Set current color based on board region
      switch (_hex.hexArea) {
      case AREA_BOARD:
         gfx.setColor(Color.LIGHT_GRAY);
         break;
      case AREA_FIRE_GOAL:
         gfx.setColor(Color.MAGENTA);
         break;
      case AREA_ICE_GOAL:
         gfx.setColor(Color.CYAN);
         break;
      case AREA_FIRE_HOME:
         gfx.setColor(Color.RED);
         break;
      case AREA_ICE_HOME:
         gfx.setColor(Color.BLUE);
         break;
      default:
         // We should never get here
         gfx.setColor(Color.WHITE);
         break;
      }
      
      // Outline the hex area
      gfx.drawPolygon(xPoints, yPoints, xPoints.length);
   }
}
